package com.kotlin.mvpdemo.ui.users;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.kotlin.mvpdemo.R;
import com.kotlin.mvpdemo.base.BaseFragment;
import com.kotlin.mvpdemo.databinding.FragmentUserListBinding;
import com.kotlin.mvpdemo.entity.ChaptersEntity;
import com.kotlin.mvpdemo.ui.users.adapter.UserListAdapter;
import com.kotlin.mvpdemo.ui.users.presenter.UserListPresenter;
import com.kotlin.mvpdemo.ui.users.view.UserListView;

import java.util.List;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/6/6
 * desc   :
 */
public class UserListFragment extends BaseFragment<UserListView, UserListPresenter, FragmentUserListBinding> implements UserListView {
    private static final String TAG = "UserListFragment";
    private int position;
    private UserListAdapter mAdapter;

    @Override
    protected UserListPresenter getPresenter() {
        return new UserListPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_user_list;
    }

    @Override
    protected FragmentUserListBinding getViewBind(LayoutInflater inflater, ViewGroup container) {
        return FragmentUserListBinding.inflate(inflater, container, false);
    }

    public static UserListFragment newInstance(int position) {
        Bundle args = new Bundle();
        args.putInt("position", position);
        UserListFragment fragment = new UserListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getArguments() == null) {
            return;
        }
        position = getArguments().getInt("position");

        Log.i(TAG, "onActivityCreated: " + position);

        mAdapter = new UserListAdapter(getContext());
        mViewBinding.mRecycleView.setHasFixedSize(true);
        mViewBinding.mRecycleView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        mViewBinding.mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        mViewBinding.mRecycleView.setAdapter(mAdapter);
        mViewBinding.tvUserListPosition.setText(position + "");

        mPresenter.getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: " + position);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: " + position);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: " + position);
    }

    @Override
    public void userList(List<ChaptersEntity> entities) {
        mAdapter.setDatas(entities);
    }
}
